package com.epoint.business;

import com.epoint.Vo.PrsAccExportFileVo;
import com.epoint.excleutil.ExcelStreamUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by wangchao on 2017/11/15.
 */
public class CreateExcelStreamUtil {

    /**
     *
     * @Description: 获取文件压缩流方法
     * @param byteStreamList List集合(多个map集合)
     * @return 返回压缩流
     * @author hbw
     * @date 2017年10月27日 上午11:20:32
     */
    public static ByteArrayOutputStream getExcelStream (List<Map<String, Object>> byteStreamList){
        //多个文件流进行压缩
        ByteArrayOutputStream  bos=null;
        ZipOutputStream zip = null;
        try{
            //压缩文件字节流
            bos = new ByteArrayOutputStream();
            zip = new ZipOutputStream(bos);
            //循环写入每个压缩项
            if (byteStreamList !=null && byteStreamList.size()>0) {
                for(Map<String,Object> stream : byteStreamList){
                    ByteArrayOutputStream byteStream = ((ByteArrayOutputStream)stream.get(ExcelStreamUtil.EXCEL_STREAM));
                    try{
                        ZipEntry ze= new ZipEntry((String) stream.get(ExcelStreamUtil.ORIGIN_EXCEL_NAME));
                        zip.putNextEntry(ze);
                        zip.write(byteStream.toByteArray());
                        zip.closeEntry();
                    }catch(Exception e){
                        throw new RuntimeException("文件压缩处理异常",e);
                    }finally{
                        if(byteStream != null){
                            try {
                                byteStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }finally{
            if(zip!= null){
                try {
                    zip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bos;
    }


    /**
     *
     * @Description: 生成Excel文件压缩包到服务并将处理结果入库
     * @param byteStreamList excelMap list集合
     * @param allData 处理的数据总数
     * @param prsAccExportFileVo
     * @param flag 是否成功标识
     * @author hbw
     * @date 2017年11月1日 上午9:20:49
     */
    private void createExcelFile (List<Map<String, Object>> byteStreamList, int allData, PrsAccExportFileVo prsAccExportFileVo,boolean flag) {
        updateExportFileVo = new PrsAccExportFileVo();
        String fileUrl = null;
        //多个文件流进行压缩
        ByteArrayOutputStream  bos=null;
        String fileName = prsAccExportFileVo.getFileName();
        try {
            //获取文件压缩流
            bos = CreateExcelStreamUtil.getExcelStream(byteStreamList);
            //上传文件
            FileInfoVO info = new FileInfoVO();
            info.setOldFileName(fileName+".zip");
            info.setSubSys("PRS");
            info.setFileStatus("0");
            info.setFilePath("excel");
            byte[] fileByte = bos.toByteArray();
            fileUrl = fileService.uploadFile(fileByte, info);
            updateExportFileVo.setProductTime(new Date());
            logger.info("upload result ：" + fileUrl);
        } catch (Exception e) {
            flag = false;
            logger.error("用户:"+user.getEmpCode()+"在"+fileName+"页面生成Excel表格失败",e);
            e.printStackTrace();
        }
        if (flag) {
            //文件生成功
            updateExportFileVo.setExportStatus(2);
        }else {
            //文件生成失败
            updateExportFileVo.setExportStatus(3);
        }
        updateExportFileVo.setId(prsAccExportFileVo.getId());
        updateExportFileVo.setFileUrl(fileUrl);
        updateExportFileVo.setExportNum(allData);
        this.prsAccExportFileBiz.updateExportFileInfo(updateExportFileVo);
    }
}
